<?php

/**
 * Created by PhpStorm.
 * User: YONBIN
 * Date: 2020/11/25
 * Time: 10:35
 * Email: 479703456@qq.com
 */

namespace app\domain\admin;

use app\model\AdminGroup;
use app\model\AdminMenus;
use app\model\AdminGroupMenusRelation;
use app\model\AdvPosition;
use app\model\ArticleCategory;
use app\model\Car;
use app\model\User;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\response\Json;

class CommonDomain
{
    public static $group_id = 0;
    public static $user_id = 0;
    public $statusCode = 200;
    public $errMsg = 'common.success';

    public function __construct()
    {
        self::$group_id = request()->currentGid ?? null;
        self::$user_id = request()->currentUid ?? null;
    }

    public function admin_menus($type): array
    {
        try {
            if ('all' == $type) {
                $list = app(AdminMenus::class)->field(['*', 'title' => 'label', 'id' => 'key'])->order('sort ASC,id ASC')->select();
            } else {
                $relations = app(AdminGroupMenusRelation::class)->where(['group_id' => self::$group_id])->column('menu_id');
                $list = app(AdminMenus::class)->whereIn('id', $relations)->order('sort ASC,id ASC')->select();
            }

            $list = commonRecursion($list, 0, 0, 'pid', 'children', true);

            return [
                'list' => $list
            ];
        } catch (\Exception $e) {
            $this->errMsg = $e->getMessage();
            return [];
        }
    }

    public function admin_group(): array
    {
        try {
            $list = app(AdminGroup::class)->select();

            return [
                'list' => $list
            ];
        } catch (\Exception $e) {
            $this->errMsg = $e->getMessage();
            return [];
        }
    }

    public function getMenuList(): array
    {
        try {
            $menuIds = app(AdminGroupMenusRelation::class)->where(['group_id' => self::$group_id])->column('menu_id');
            $list = app(AdminMenus::class)
                ->where(['status' => 'enable'])
                ->whereIn('id', $menuIds)
                ->field(['*', 'id' => 'key', 'title' => 'label'])
                ->order('sort ASC,id DESC')
                ->select();
            $result = [];
            $list->filter(function ($row) use (&$result) {
                $result[] = [
                    'id' => $row['id'],
                    'pid' => $row['pid'],
                    'path' => $row['route'],
                    'name' => $row['component_name'],
                    'component' => 0 === $row['pid'] ? 'LAYOUT' : $row['component'],
                    'redirect' => $row['redirect'],
                    'meta' => [
                        'id' => $row['id'],
                        'title' => $row['title'],
                        'icon' => $row['icon'],
                    ]
                ];
            });

            return commonRecursion($result, 0, 0, 'pid', 'children', false);
        } catch (\Exception $e) {
            $this->errMsg = $e->getMessage();
            return [];
        }
    }
}